package com.tomdd.router;

import com.tomdd.message.WxMessage;
import com.tomdd.message.WxOutMessage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 消息路由
 *
 * @author zx
 * @date 2025-03-12 13:46
 */
@AllArgsConstructor
@Slf4j
public class WxMpMessageRouter {
    private final List<WxMpMessageRouterRule> rules = new ArrayList<>();

    public List<WxMpMessageRouterRule> getRules() {
        return this.rules;
    }

    /**
     * 开始一个新的路由规则。
     *
     * @return the wx mp message router rule
     */
    public WxMpMessageRouterRule rule() {
        return new WxMpMessageRouterRule(this);
    }

    public WxOutMessage route(final WxMessage wxMessage, final Map<String, Object> context) {
        WxOutMessage outMessage = null;
        for (WxMpMessageRouterRule rule : this.getRules()) {
            if(rule.test(wxMessage)){
                outMessage = rule.service(wxMessage, context);
                break;
            }
        }

        return outMessage;
    }
}
